package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbContentsVo;
import com.ruoyi.tb.lmt.domain.vo.TbLmtTypeRepairTimeVo;
import com.ruoyi.tb.lmt.mapper.TbContentsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbLmttypeBo;
import com.ruoyi.tb.lmt.domain.vo.TbLmttypeVo;
import com.ruoyi.tb.lmt.domain.TbLmttype;
import com.ruoyi.tb.lmt.mapper.TbLmttypeMapper;
import com.ruoyi.tb.lmt.service.ITbLmttypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 大机型号Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-30
 */
@RequiredArgsConstructor
@Service
public class TbLmttypeServiceImpl implements ITbLmttypeService {

    private final TbLmttypeMapper baseMapper;
    private final TbContentsMapper tbContentsMapper;

    /**
     * 查询大机型号
     */
    @Override
    public TbLmttypeVo queryById(Long ID) {
        TbLmttypeVo vo = baseMapper.selectVoById(ID);

        return vo;
    }

    /**
     * 查询大机型号列表
     */
    @Override
    public TableDataInfo<TbLmttypeVo> queryPageList(TbLmttypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbLmttype> lqw = buildQueryWrapper(bo);
        // 在这里添加根据 CreateTime 降序排序
        pageQuery.setOrderByColumn("createTime");
        pageQuery.setIsAsc("desc");
        Page<TbLmttypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (TbLmttypeVo vo : result.getRecords()) {
            TbContentsVo two = tbContentsMapper.getById(vo.getTwoC());
            if (two != null) {
                vo.setTwoCName(two.getName());
            }
            TbContentsVo three = tbContentsMapper.getById(vo.getThreeC());
            if (three != null) {
                vo.setThreeCName(three.getName());
            }
            TbContentsVo four = tbContentsMapper.getById(vo.getFourC());
            if (four != null) {
                vo.setFourCName(four.getName());
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询大机型号列表
     */
    @Override
    public List<TbLmttypeVo> queryList(TbLmttypeBo bo) {
        LambdaQueryWrapper<TbLmttype> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbLmttype> buildQueryWrapper(TbLmttypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbLmttype> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLmtType()), TbLmttype::getLmtType, bo.getLmtType());
        lqw.like(StringUtils.isNotBlank(bo.getAlias()), TbLmttype::getAlias, bo.getAlias());
        lqw.orderByAsc(TbLmttype::getSeqNo);
        return lqw;
    }

    /**
     * 新增大机型号
     */
    @Override
    public Boolean insertByBo(TbLmttypeBo bo) {
        TbLmttype add = BeanUtil.toBean(bo, TbLmttype.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改大机型号
     */
    @Override
    public Boolean updateByBo(TbLmttypeBo bo) {
        TbLmttype update = BeanUtil.toBean(bo, TbLmttype.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbLmttype entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除大机型号
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Long countAll() {
        return baseMapper.selectCount(null);
    }

    @Override
    public List<TbLmtTypeRepairTimeVo> queryAllRepairTimeList() {
        return baseMapper.queryAllRepairTimeList();
    }

    @Override
    public TbLmttypeVo selectByLmtType(String lmtType) {
        return baseMapper.selectByLmtType(lmtType);
    }
}
